﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Text;
using SDK;
using System.Data;
using Common;
using eBay;

namespace eBayMonitorer {
    static class main {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new mainFrm());

            Global.FilleBaySiteNames();
        }

        public static void getCategories() {
            XmlDocument categories = null;
            try {
                categories = Categories.GetCategories();
            }
            catch (Categories.CouldNotEstablishLatestVersionNumber) { MessageBox.Show("Program was unable to get the latest version of categories dictionary available online"); return; }
            catch (Categories.CouldNotObtainCategoryTree) { MessageBox.Show("Program was unable to get the latest categories dictionary available online"); return; }


            if (categories != null) {
                XmlNode categoriesNode = categories["GetCategoriesResponse"]["CategoryArray"];

                //go through each category
                StringBuilder rets = new StringBuilder();
                rets.Append("Categories:\n");
                foreach (XmlNode cat in categoriesNode.ChildNodes) {
                    //if this category has a categoryID equal to its parent category id
                    //then it is a main category
                    if (cat.Name == "Category" && cat["CategoryParentID"].InnerText == cat["CategoryID"].InnerText) {
                        rets.Append("\t" + cat["CategoryName"].InnerText + " (" + cat["CategoryID"].InnerText + ")\n");
                    }
                }
                MessageBox.Show(rets.ToString());
            }
        }

        public static void GeteBayOfficialTime() {
            DateTime date = eBay.API.OfficialTime.GetOfficialTime();
            MessageBox.Show(date.ToString());
        }

        public static void FindItems
            (string QueryKeywords, SortOrderCodeType SortOrder, int MaxEntries, bool HideDuplicateItems, string PostalCode, SimpleItemSortCodeType ItemSort) {
            FindItemsResponseType response = eBay.API.ShoppingAPI.FindItems(QueryKeywords, SortOrder, MaxEntries, HideDuplicateItems, PostalCode, ItemSort);

            if (response != null && response.Items != null) {

                StringBuilder rets = new StringBuilder();
                rets.Append("Items:\n");
                foreach (DataRow ritem in response.Items) {
                    //if this category has a categoryID equal to its parent category id
                    //then it is a main category
                    rets.Append("\t" + ritem["Title"] + "\n\t\t Item ID: " + ritem["ItemID"] + "\n" + "\t\tEnd Time: " + DateTime.Parse(ritem["EndTime"] + "").ToString() + "\n\n");
                }
                MessageBox.Show(rets.ToString());
            }
            else
                MessageBox.Show("Error happened");
        }

        public static void FindItemsAdvanced(Dictionary<string, object> parameters) {

        }
    }
}
